package com.ruitu.health_1.mvp.presenter;

import com.ruitu.arad.base.BasePresenter;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_1.mvp.model.ShoppingCartModel;
import com.ruitu.health_1.ui.fragment.ShoppingCartFragment;
import com.ruitu.router_module.bean.BaseResult2;
import com.ruitu.router_module.bean.cart.Cart;
import com.ruitu.router_module.bean.order.ConfirmOrder;
import com.ruitu.router_module.bean.product.Sku;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

public class ShoppingCartPresenter {

    public static class ShoppingCartP extends BasePresenter<ShoppingCartFragment, ShoppingCartModel.ShoppingCartM> {
        public Disposable cartList(String memberId) {

            return m.cartList(memberId).subscribe(new Consumer<BaseResult2<List<Cart>>>() {
                @Override
                public void accept(BaseResult2<List<Cart>> baseResult2) throws Exception {
                    if (baseResult2.isSuccess()) {
                        v.setData(baseResult2.getData());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {

                }
            });
        }

        public Disposable deleteCart(RequestBody body) {

            return m.deleteCart(body).subscribe(new Consumer<BaseResult2>() {
                @Override
                public void accept(BaseResult2 baseResult2) throws Exception {
                    v.hideProgress();
                    if (baseResult2.isSuccess()) {
                        v.deleteSuc();
                    }
                    ToastUtils.showShort(baseResult2.getMessage());
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                }
            });
        }

        public Disposable settlement(RequestBody body) {
            return m.settlement(body).subscribe(new Consumer<BaseResult2<ConfirmOrder>>() {
                @Override
                public void accept(BaseResult2<ConfirmOrder> baseResult2) throws Exception {
                    v.hideProgress();
                    if (baseResult2.isSuccess()) {
                        v.setSettlement(baseResult2.getData());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    throwable.printStackTrace();
                }
            });
        }

        public Disposable changeQuantity(RequestBody body) {
            return m.changeQuantity(body)
                    .subscribe(new Consumer<BaseResult2>() {
                        @Override
                        public void accept(BaseResult2 baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.changeSuc();
                            }
                            ToastUtils.showShort(baseResult2.getMessage());
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }

        public Disposable getProduct(String productId) {
            return m.getProductSku(productId)
                    .subscribe(new Consumer<BaseResult2<List<Sku>>>() {
                        @Override
                        public void accept(BaseResult2<List<Sku>> baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.getSkuList(baseResult2.getData());
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }

        public Disposable updateSku(RequestBody body) {
            return m.updateSku(body)
                    .subscribe(new Consumer<BaseResult2>() {
                        @Override
                        public void accept(BaseResult2 baseResult2) throws Exception {
                            v.hideProgress();
                            if (baseResult2.isSuccess()) {
                                v.updateSuc();
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                        }
                    });
        }
    }
}
